<template>
    <view class="login-page">
        <view class="login-bg" :style="{backgroundImage: 'url(' + pic_url + ')'}"></view>
        <view class="tip">
            <view>为了您能便捷的使用煌上煌服务，需要获取您的手机号，以便于一键注册登录，谢谢！</view>
        </view>
        <view class="login-btn">
            <button class="button" type="primary" hover-class="none" open-type="getPhoneNumber"
                @getphonenumber="getPhoneNumber">
                <image :src="wxIcon"></image>
                <text>微信用户一键登录</text>
            </button>
        </view>
    </view>
</template>

<script>
    import loginServer from '@/plugins/http/server/login'
    const { getOpenId, getPhone, minRegCustomer } = loginServer
    import { appkey, platCode, source } from '@/assets/config'
    export default {
        data() {
            return {
                wxIcon: require('@/assets/images/wechat_white.png'),
                pic_url: require('@/assets/images/loginbg.png'),
            }
        },
        onLoad(query) {
            console.log(query)
        },
        methods: {
            login(cb) {
                uni.login({
                    provider: 'weixin',
                    success(res) {
                        if (res.code) {
                            cb && cb(res)
                        } else {
                            console.log('登录失败！' + res.errMsg)
                        }
                    },
                    fail() {
                        console.log('err')
                    }
                })
            },
            getPhoneNumber(res) {
                if (res.detail.errMsg != 'getPhoneNumber:ok') {
                    return false;
                }
                const { encryptedData, iv } = res.detail;
                uni.showLoading({
                    title: '一键登录中...',
                    mask: true
                })
                this.login(resCode => {
                    const { code } = resCode
                    const platformWxappletId = '12'
                    getOpenId({ code, platformWxappletId }).then(res => {
                        const data = JSON.parse(res.data)
                        const { session_key, openid } = data
                        const phoneParams = { encryptedData, iv, session_key }
                        getPhone(phoneParams).then(res => {
                            const { phoneNumber, watermark: { appid } } = res.data
                            const wechatInfo = {
                                openId: openid,
                                phone: phoneNumber,
                                appId: appid
                            }
                            this.$store.commit('commitWechatInfo', wechatInfo)
                            const regParams = {
                                appkey, //系统授权密钥
                                fullName: phoneNumber, //姓名
                                mobile: phoneNumber, //手机号码
                                platCode, //平台编号
                                source //来源 老会员/门店/异业合作
                            }
                            minRegCustomer(regParams).then(res => {
                                uni.hideLoading()
                                uni.reLaunch({
                                    url: '/pages/home/index'
                                })
                            }).catch(e => {
                                uni.hideLoading()
                                console.log(e)
                                uni.showToast({
                                    icon: 'none',
                                    title: '一键登录失败',
                                });
                            })
                        }).catch(e => {
                            uni.hideLoading()
                            console.log(e)
                            uni.showToast({
                                icon: 'none',
                                title: '一键登录失败',
                            });
                        })
                    }).catch(e => {
                        uni.hideLoading()
                        console.log(e)
                    })
                })
            },
        }
    }
</script>

<style lang="less" scoped>
    @import 'index';
</style>
